<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Config;
use think\Hook;
use think\Validate;

use think\Session;

use app\admin\library\Auth;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

use think\Log;


use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
//use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;


use fast\Http;

use GatewayClient\Gateway;


class Thm extends Frontend
{

    protected $layout = 'default';
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];

    const IMEI_NAME = "__imeiname__";
    const AQM_NAME = "__aqmname__";

    protected $imei = "";
    protected $aqm = "";//安全码

    protected $thmuser = null;//关联的fa_thm_user
    protected $thmapp = null;//关联的fa_thm_app

    public function _initialize()
    {
        parent::_initialize();

        $this->imei = $this->request->request('imei', "");
        $ip = $this->request->ip();
        if (!empty($this->imei)) {
            Session::set(self::IMEI_NAME, $this->imei);
        } else {
            $this->imei = Session::get(self::IMEI_NAME);
        }

        if (empty($this->imei)) {
            $this->error("imei为空请登录！");
        }

        $this->aqm = Session::get(self::AQM_NAME);

        $this->thmuser = \app\admin\model\thm\User::where('name', $this->imei)->find();
        if ($this->thmuser) {
            $this->thmapp = \app\admin\model\thm\App::where('id', $this->thmuser->app_id)->find();
            $this->thmuser = $this->thmuser->toArray();
        }

        if (!$this->thmapp) {
            $this->thmapp = \app\admin\model\thm\App::where('id', ">", 0)->limit(1)->find();
        }

//        $uinfo = \app\admin\model\thm\Userinfolog::where('thm_user_id', $this->thmuser['id'])->find();
//        //设置类型图片
//        if ($uinfo) {
//            if ($this->thmapp['a6_name1'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image1'];}
//            if ($this->thmapp['a6_name2'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image2'];}
//            if ($this->thmapp['a6_name3'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image3'];}
//            if ($this->thmapp['a6_name4'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image4'];}
//            if ($this->thmapp['a6_name5'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image5'];}
//            if ($this->thmapp['a6_name6'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image6'];}
//            if ($this->thmapp['a6_name7'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image7'];}
//            if ($this->thmapp['a6_name8'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image8'];}
//            if ($this->thmapp['a6_name9'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image9'];}
//            if ($this->thmapp['a6_name10'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image10'];}
//            if ($this->thmapp['a6_name11'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image11'];}
//            if ($this->thmapp['a6_name12'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image12'];}
//            if ($this->thmapp['a6_name13'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image13'];}
//            if ($this->thmapp['a6_name14'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image14'];}
//            if ($this->thmapp['a6_name15'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image15'];}
//            if ($this->thmapp['a6_name16'] == $uinfo['type']) { $this->thmapp['title_image'] = $this->thmapp['a6_image16'];}
//        }


        $this->thmapp = $this->thmapp->toArray();

        $this->assignconfig('thmappaqm', $this->thmapp['aqm']);

        $this->view->assign("thmapp", $this->thmapp);
        $this->view->assign("thmuser", $this->thmuser);

    }

    /**
     * 首页
     */
    //http://fastaws.test/index/thm/index?imei=29c907b36efc5e72
//http://test.megowx.com//index/thm/index?imei=29c907b36efc5e72
    public function index()
    {
        if (empty($this->aqm)) {
            header("Location:sqy");
        }

        $this->view->assign('title', __('首頁'));
        return $this->view->fetch();
    }


    public function Selecttype()
    {
        $this->view->assign('title', __('类型'));
        return $this->view->fetch();
    }

    public function Button1()
    {
        $this->view->assign('title', __('查询'));
        return $this->view->fetch();
    }

    //http://fastaws.test/index/thm/Button2
    public function Button2()
    {
        if ($this->request->isPost()) {
            $_POST['thm_user_id'] = $this->thmuser['id'];

            $uinfo = new \app\admin\model\thm\Userinfolog();
            $uinfo->allowField(true)->save($_POST);
            $this->success("信息上传成功！", url('thm/index'));
        }

        $type = $this->request->request('type', 1);
        $typename = "";
        switch($type) {
            default:
            case 1:
                $typename = $this->thmapp['a6_name1'];
                $typeimage = $this->thmapp['a6_image1'];
                break;
            case 2:
                $typename = $this->thmapp['a6_name2'];
                $typeimage = $this->thmapp['a6_image2'];
                break;
            case 3:
                $typename = $this->thmapp['a6_name3'];
                $typeimage = $this->thmapp['a6_image3'];
                break;
            case 4:
                $typename = $this->thmapp['a6_name4'];
                $typeimage = $this->thmapp['a6_image4'];
                break;
            case 5:
                $typename = $this->thmapp['a6_name5'];
                $typeimage = $this->thmapp['a6_image5'];
                break;
            case 6:
                $typename = $this->thmapp['a6_name6'];
                $typeimage = $this->thmapp['a6_image6'];
                break;
            case 7:
                $typename = $this->thmapp['a6_name7'];
                $typeimage = $this->thmapp['a6_image7'];
                break;
            case 8:
                $typename = $this->thmapp['a6_name8'];
                $typeimage = $this->thmapp['a6_image8'];
                break;
            case 9:
                $typename = $this->thmapp['a6_name9'];
                $typeimage = $this->thmapp['a6_image9'];
                break;
            case 10:
                $typename = $this->thmapp['a6_name10'];
                $typeimage = $this->thmapp['a6_image10'];
                break;
            case 11:
                $typename = $this->thmapp['a6_name11'];
                $typeimage = $this->thmapp['a6_image11'];
                break;
            case 12:
                $typename = $this->thmapp['a6_name12'];
                $typeimage = $this->thmapp['a6_image12'];
                break;
            case 13:
                $typename = $this->thmapp['a6_name13'];
                $typeimage = $this->thmapp['a6_image13'];
                break;
            case 14:
                $typename = $this->thmapp['a6_name14'];
                $typeimage = $this->thmapp['a6_image14'];
                break;
            case 15:
                $typename = $this->thmapp['a6_name15'];
                $typeimage = $this->thmapp['a6_image15'];
                break;
            case 16:
                $typename = $this->thmapp['a6_name16'];
                $typeimage = $this->thmapp['a6_image16'];
                break;

        }

        $this->view->assign('typeimage', $typeimage);
        $this->view->assign('type', $typename);
        $this->view->assign('title', __('用戶資訊'));
        return $this->view->fetch();
    }

    public function Button3()
    {
        if ($this->request->isPost()) {
            $_POST['thm_user_id'] = $this->thmuser['id'];

            $uinfo = new \app\admin\model\thm\Liuyan();
            $uinfo->allowField(true)->save($_POST);
            $this->success("留言上传成功！", url('thm/index'));
        }

        $this->view->assign('title', __('留言'));
        return $this->view->fetch();
    }

    public function searchimage($name)
    {
        if ($this->request->isPost()) {
            $image = \app\admin\model\thm\Image::where("name", $name)->find();

            if ($image) {
                $this->success("查询图片成功！", null, $image->image);
            } else {
                $this->error($this->thmapp['a2_warning']);
            }
        }
    }

    //http://fastaws.test/index/thm/showpic?picurl=122
    public function showpic($picurl)
    {
        $this->view->assign('picurl', $picurl);
        $this->view->assign('title', __('图'));
        return $this->view->fetch();
    }


    //http://fastaws.test/index/thm/test
    public function test()
    {

        if ($this->request->isPost()) {
            $a = 0;
            $this->success("查询图片成功！");
        }
        $this->view->assign('title', __('test'));
        return $this->view->fetch();
    }

    //http://fastaws.test/index/thm/test2
    public function test2()
    {
        $this->view->assign('title', __('test'));
        return $this->view->fetch();
    }

    //http://fastaws.test/index/thm/test3
    public function test3()
    {
        $this->view->assign('title', __('test'));
        return $this->view->fetch();
    }

    //http://fastaws.test/index/thm/test4
    //Canvas绘制网格
    public function test4()
    {

        $this->view->assign('title', __('test'));
        return $this->view->fetch();
    }

    //http://fastaws.test/index/thm/index?imei=29c907b36efc5e72
    //http://fastaws.test/index/thm/index/sqy
    public function sqy()
    {
        if ($this->request->isPost()) {

            if ( strcmp($_POST['aqm'], $this->thmapp['aqm']) !== 0) {
                $this->error("安全碼错误！");
            }
            Session::set(self::AQM_NAME, $_POST['aqm']);
            $this->success("安全碼正确！");
        }

        $this->view->assign('title', __('授權頁'));
        return $this->view->fetch();
    }


}
